package com.tianqu.tms.www.user.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.entity.User;
import com.tianqu.tms.entity.UserAuth;
import com.tianqu.tms.entity.UserClass;
import com.tianqu.tms.entity.UserRole;
import com.tianqu.tms.www.user.action.UserUDDetailAction;
import com.tianqu.tms.www.user.actionform.UserInputActionForm;
import com.tianqu.tms.www.user.viewhelper.UserDetailViewHelper;

/**
 * 用户信息详细页面
 * 
 * @author tianshufu
 * 
 */
public class UserUDDetailController extends TmsController {

	private Logger logger = Logger.getLogger(UserUDDetailAction.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");

		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();

			UserInputActionForm viewForm = (UserInputActionForm) uc
					.getInputForm();

			// 根据主键ID到数据库中查询数据
			User entity = new User();
			entity = entity.getUserInfoById(viewForm.getId());

			UserDetailViewHelper view = new UserDetailViewHelper();

			view.setId(String.valueOf(entity.getId()));

			view.setLoginName(entity.getLoginId());

			view.setUserName(entity.getRealName());
			
			view.setUserType(entity.getUserType());
			
			view.setFlag(entity.getFlag());
			
			
			// 从字典表中查出用户类型信息
			UserClass code = UserClass.getInstance();
			view.setUserType(code.getUserClassNameByUserClass(Integer
					.parseInt(entity.getUcId())));

			view.setUcId(entity.getUcId());
			
			if(entity.getFlag()==1)
			{	
			  view.setFlags("正常");
			}
			if(entity.getFlag()==2)
			{	
			  view.setFlags("删除");
			}
			if(entity.getFlag()==3)
			{	
			  view.setFlags("禁用");
			}
            
			view.setInsertTime(DateUtil.getDateByTimeStamp(entity.getInsertTime()));
			
			view.setUcId(entity.getUcId());
			UserAuth userAuth = UserAuth.getInstance();
			String id = (String)uc.getAttribute("id");
			List<UserAuth> userAuthList = userAuth.getAuth(Integer.parseInt(id));
			
			// 根据userClass查询roleId
			UserRole role = UserRole.getInstance();
			List<UserRole> roleList = role.getRoleinfoByUcId(Integer
					.parseInt(entity.getUcId()),2);
			List<UserRole> roleList1 = new ArrayList<UserRole>();
			int used = 2;
			if(userAuthList != null && userAuthList.size() > 0){
				for(UserRole uRole : roleList){
					used = getRoleUsed(uRole.getRole(),userAuthList);
					if(used == 1){
						roleList1.add(uRole);
					}
				}
				
				
			}	
	
			
			
			view.setRoleList(roleList1);
			// userprofile初始化 获取登录者类型信息
//			TmsUserProfile userprofile = uc.getUserProfile();

//			if (userprofile.getUserClass() == 90) {
//				// 管理员登录 并且管理员不能删除自己
//				if (Integer.parseInt(entity.getUcId()) == userprofile
//						.getUserClass()) {
//					view.setButtonDisplay(2);
//				} else {
//					view.setButtonDisplay(1);
//				}
//				// 其他账户不能删除root账户
//				if (entity.getLoginId().equals("root")) {
//					view.setButtonDisplay(2);
//				}
//			} else {
//
//				// 操作者登录
//				view.setButtonDisplay(2);
//			}

			
			
			uc.setAttribute("view", view);// 用于前台显示

			logger
					.debug(this.getClass().getName()
							+ "#doAction was completed.");

			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}
	private int getRoleUsed(int role,List authList){
		int used=2;
		for(int i=0;i<authList.size();i++){
			UserAuth userAuth = UserAuth.getInstance();
			userAuth = (UserAuth)authList.get(i);
			if(userAuth.getrId() == role){
				return used = 1;
			}
		}
		return used;
	}
}
